package com.xindaoapp.happypet.ui;

import java.io.File;

import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;

import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.activity.PictureListActivity;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.utils.Constants.PUBLISHPOSTS;

/**
 * 首页--中间打按钮--选择发帖模式
 * 
 * @author xindaoapp
 * 
 */
public class SelectPostModeActivity extends BaseActivity implements OnClickListener {
    public String mCameraFilePath;

    @Override
    protected void initViews() {
        super.initViews();
    }

    @Override
    protected void initEvents() {
        super.initEvents();
        findViewById(R.id.tv_text).setOnClickListener(this);
        findViewById(R.id.tv_camera).setOnClickListener(this);
        findViewById(R.id.tv_photo).setOnClickListener(this);
        findViewById(R.id.close).setOnClickListener(this);
    }

    @Override
    protected int getContentViewResId() {
        return R.layout.activity_select_post_mode;
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.tv_text:
                intent = new Intent(getBaseContext(), PostMessageActivity.class);
                startActivity(intent);
                finish();
                break;
            case R.id.tv_camera:
                // intent = new Intent(getBaseContext(), CameraActivity.class);
                // startActivity(intent);
                takePhoto();
                break;
            case R.id.tv_photo:
                intent = new Intent(getBaseContext(), PictureListActivity.class);
                intent.putExtra(Constants.KEY_SELECT_PICTURE_MODE, true);
                startActivity(intent);
                finish();
                break;
            case R.id.close:
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PUBLISHPOSTS.CAMERA && resultCode == RESULT_OK) {// 拍照完成
            // 跳转发帖页面，并传递图片地址
            Intent intent = new Intent(getBaseContext(), PostMessageActivity.class);
            intent.putExtra(Constants.KEY_CAMERA_PICTURE_PATH, mCameraFilePath);
            startActivity(intent);
            finish();
        }
    }

    /**
     * 拍照
     */
    private void takePhoto() {

        mCameraFilePath =
                Environment.getExternalStorageDirectory().getAbsolutePath() + "/happypet/pic"
                        + File.separator + System.currentTimeMillis() + ".jpg";
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(mCameraFilePath)));
        startActivityForResult(intent, PUBLISHPOSTS.CAMERA);

    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }
}
